<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
        <script type="text/javascript">
            window.addEventListener('message', function(event){
                // 通过origin属性判断消息来源地址
                if (event.origin == 'http://a.com') {
                    alert(event.data);    // 弹出"I was there!"
                    alert(event.source);  // 对a.com、index.html中window对象的引用
                                          // 但由于同源策略，这里event.source不可以访问window对象
                }
            }, false);
        </script>
</body>
</html>